<template>
	<view class="container">
		<view class="">
			<image class="shangpintupian" :src="detailitem.goodsimg" mode="widthFix"></image>
		</view>
		<!-- 获取商品id -->
		<!-- <text>商品id：{{goods_info.goods}}</text> -->
		<!-- 商品信息 -->
		<view class="goods-info">
			<!-- 商品价格 -->
			<view class="goods-info-price">
				￥<text>{{detailitem.goodsprice}}</text>
			</view>
			<!-- 商品名称 -->
			<view class="goods-info-title">
				<text>{{detailitem.imgtitle}}</text>
			</view>
			<view class="goods-info-particulars">
				<text>{{detailitem.goodsparticulars}}</text>
			</view>
		</view>
		<!-- 购买用户评价-->
		<!-- 
			客服  店铺   购物车        加入购物车    立即购买
			四个tabbar页面中   购物车  导航   徽章  （购物车几件商品）
			
			单击“加入购物车”按钮    拼凑一个商品对象  const   goods = {}
			mutations  中方法
		 -->
		<!--  商品导航-->
		<view class="goods-nav">
			<uni-goods-nav :options="options" :fill="true" :button-group="customButtonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
		<!-- 购买导航 -->
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	export default {
		data() {
			return {
				// 商品导航部分数据
				options: [{
						icon: "phone",
						text: "客服"
					},
					{
						icon: "home",
						text: "店铺"
					},
					{
						icon: "cart",
						info: 0,
						text: "购物车"
					}
				],
				customButtonGroup: [{
						text: "加入购物车",
						backgroundColor: "#ccb89f",
						color: "#fff"
					},
					{
						text: "立即购买",
						backgroundColor: "red",
						color: "#fff"
					}
				],
				// 测试
				// test:""
				// 当前详情对应的商品
				goods_info: {},
				goodsdetail: [],
				detailitem: [],

			};
		},
		computed: {
			// ...   扩展运算符   形式上展开，以逗号为分隔符的字符串
			// ...   进行数组合并
			// mapGetters  将total映射到购物车里面
			// mapGetters("模块名",[getters中方法名，getters中方法名，...])
			// mapGetters("getters中方法名"，"getters中方法名"，...])
			...mapGetters("m_car", ["total"])


		},
		// 当页面打开的时候，购物车显示右上角徽标
		onShow() {
			uni.setTabBarBadge({
				index: 2,
				text: this.total + ""
			})
		},
		onLoad(options) {
			console.log(options);
			this.goods_info = options
			// console.log(this.goods_info);
			this.options[2].info = this.total

			this.getgoodsdetails()
		},
		watch: {
			total(newValue, oldValue) {
				const res1 = this.options.find(x => x.text == "加入购物车")
				if (res1) {
					res1.info = newValue
				}
			}
		},
		methods: {
			// 左侧options  店铺购物车
			onClick(e) {
				// console.log(e);
				// e.index   左侧options数组的元素下标
				// e.content  左侧options数组元素对象
				if (e.content.text == "购物车") {
					uni.switchTab({
						url: "/pages/car/car"
					})
				}
			},
			// 右侧custobuttons 加入购物车
			buttonClick(e) {
				console.log('e', e);
				if (e.content.text == "加入购物车") {
					// 构造一个商品对象
					const goods = {
						goods_id: this.detailitem.goodid,
						goods_name: this.detailitem.imgtitle,
						goods_price: this.detailitem.goodsprice,
						goods_particulars: this.detailitem.goodsparticulars,
						// 用户购买的商品数量
						goods_count: 1,
						// 购物车商品状态
						goods_state: false,
						goods_img: this.detailitem.goodsimg
					}


					// 将上述商品追加到car数组

					this.$store.commit("m_car/addtocar", goods)
					uni.switchTab({
						url: "/pages/car/car"
					})
				}
			},
			async getgoodsdetails() {
				const {
					data: {
						goods: res
					}
				} = await uni.$http.get("http://localhost:3000/goodslist")
				this.goodsdetail = res.goodslist
				this.detailitem = this.goodsdetail.find(item => item.goodid == this.goods_info.goods)
				// console.log(this.detailitem);
				// console.log('res',res);
			}
		}
	}
</script>

<style lang="scss">
	.container {

		.shangpintupian {
			width: 100%;
		}

		.goods-info {
			.goods-img {}

			.goods-info-price {
				color: #eb140d;
				font-size: 20px;
				font-weight: 700;
				margin: 10px;
			}

			.goods-info-title {
				font-size: 16px;
				font-weight: 600;
				padding: 8px;
				padding-top: 0;
			}

			.goods-info-particulars {
				margin-top: 15rpx;
				font-size: 30rpx;
			}
		}

		.goods-nav {
			position: fixed;
			bottom: 0px;
			left: 0px;
			width: 100%;
		}
	}
</style>